﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Command.HelpClass;
using czc;
using Guestbook.Biz;
using Guestbook.IBiz;

namespace WebApp.Controllers
{
    public class OrganizationController : Controller
    {
        public IGroupsBiz groupsBiz = new GroupsBiz();
        //
        // GET: /Organization/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult GetList(string id)
        {
            string tiaojian = "0";
            if (!string.IsNullOrEmpty(id))
            {
                tiaojian = id;
            }

            var mql = GroupsSet.SelectAll();//.Where(GroupsSet.ParentGroupId.Equal(tiaojian));
            var mql2 = GroupsSet.SelectAll().Where(GroupsSet.ParentGroupId.Equal(tiaojian));
            List<Groups> list3 = groupsBiz.GetOwnList<Groups>(mql);
            List<Groups> list2 = groupsBiz.GetOwnList<Groups>(mql2);
            List<Spacetree> splist = new List<Spacetree>();
            foreach (Groups model in list2)
            {
                List<Spacetree> linshi = deep(model, list3);
                Spacetree spmodel = new Spacetree();
                spmodel.id = model.GroupId;
                spmodel.name = model.GroupName;
                spmodel.children = linshi;

                splist.Add(spmodel);
            }
            // Dictionary<string, object> dic = new Dictionary<string, object>();
            //ControlButtons d = new ControlButtons();

            // var mql = ControlButtonsSet.ControlId.NotEqual("");
            // dic.Add("children", list2);
            return Json(splist, JsonRequestBehavior.AllowGet);

        }
        //递归循环使用
        List<Spacetree> deep(Groups model, List<Groups> list2)
        {
            List<Spacetree> splist = new List<Spacetree>();
            List<Groups> list3 = list2.Where(fd => fd.ParentGroupId == model.GroupId).ToList();
            foreach (Groups model2 in list3)
            {
                List<Spacetree> linshilist = deep(model2, list2);
                Spacetree spmodel = new Spacetree();
                spmodel.id = model2.GroupId;
                spmodel.name = model2.GroupName;
                spmodel.children = linshilist;
                splist.Add(spmodel);
            }
            return splist;
        }

        [HttpPost]
        public JsonResult GetModle(string id)
        {
            var mql2 = GroupsSet.SelectAll().Where(GroupsSet.GroupId.Equal(id));
            Groups Gmodel = groupsBiz.GetEntity(mql2);
            return Json(Gmodel, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetCheakList()
        {
            string tiaojian = "0";
            var mql = GroupsSet.SelectAll();//.Where(GroupsSet.ParentGroupId.Equal(tiaojian));
            var mql2 = GroupsSet.SelectAll().Where(GroupsSet.ParentGroupId.Equal(tiaojian));
            List<Groups> list3 = groupsBiz.GetOwnList<Groups>(mql);
            List<Groups> list2 = groupsBiz.GetOwnList<Groups>(mql2);
            List<ComBotree> splist = new List<ComBotree>();
            foreach (Groups model in list2)
            {
                List<ComBotree> linshi = deep2(model, list3);
                ComBotree spmodel = new ComBotree();
                spmodel.id = model.GroupId;
                spmodel.text = model.GroupName;
                spmodel.children = linshi;

                splist.Add(spmodel);
            }
            return Json(splist, JsonRequestBehavior.AllowGet);
        }
        //递归循环使用
        List<ComBotree> deep2(Groups model, List<Groups> list2)
        {
            List<ComBotree> splist = new List<ComBotree>();
            List<Groups> list3 = list2.Where(fd => fd.ParentGroupId == model.GroupId).ToList();
            foreach (Groups model2 in list3)
            {
                List<ComBotree> linshilist = deep2(model2, list2);
                ComBotree spmodel = new ComBotree();
                spmodel.id = model2.GroupId;
                spmodel.text = model2.GroupName;
                spmodel.children = linshilist;
                splist.Add(spmodel);
            }
            return splist;
        }

        public JsonResult Addmodel(string name, string parentid, string description, string Flage, string GroupId)
        {

            Random rnd = new Random();
            Groups spmodel = new Groups();

            spmodel.GroupName = name;
            spmodel.GroupDescription = description;
            spmodel.ParentGroupId = parentid;
            spmodel.ModifyOn = DateTime.Today;
            spmodel.ModifyBy = "";
            spmodel.GroupOrder = 1;
            spmodel.CreateBy = "";
            spmodel.CreateOn = DateTime.Today;

            if (Flage == "1")
            {
                spmodel.GroupId = rnd.Next(10, 9999).ToString();   //得10~19的随机数;
                groupsBiz.Add(spmodel);
            }
            else
            {

                spmodel.WhereExpression = GroupsSet.GroupId.Equal(GroupId);
                //  spmodel.GroupId = GroupId;
                groupsBiz.Update(spmodel);
            }



            return Json("ok", JsonRequestBehavior.AllowGet);
        }

        public JsonResult Deletemodel(string GroupId)
        {
            DbParameter[] parameters = {
			           new SqlParameter("@GroupId", SqlDbType.NVarChar,10) 
                    
            };
            parameters[0].Value = GroupId;
            groupsBiz.ExecuteProWithNonQuery("up_delete_nclass", parameters);
            return Json("ok", JsonRequestBehavior.AllowGet);
        }


    }
}
